package cn.itcast.bos.web.action.base;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@ParentPackage("json-default")
@Controller
@Namespace("/")
@Scope("prototype")
public class CourierAction extends ActionSupport implements ModelDriven<Courier> {

    private Courier courier = new Courier();
    @Override
    public Courier getModel() {
        return courier;
    }

    @Autowired
    private CourierService courierService;

    //保存方法
    @Action(value = "courier_save",results = {@Result(name = "success",type = "redirect",location = "./pages/base/courier.html")})
    public String save(){
        courierService.save(courier);
        return SUCCESS;
    }

    //分页查询,添加属性
    private int page;
    private int rows;
    public void setPage(int page) {
        this.page = page;
    }
    public void setRows(int rows) {
        this.rows = rows;
    }

    @Action(value = "courier_pageQuery",results = {@Result(name = "success",type = "json")})
    public String query(){
        Pageable pageable = new PageRequest(page-1,rows);

       Specification<Courier> specification = new Specification<Courier>() {
            @Override
            public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                //root是构造查询条件，例如name=？
                //like是模糊查询，equal是精确查询
                ArrayList<Predicate> list = new ArrayList<>();  //条件并入集合中

                //精确查询CourierNum
                if (StringUtils.isNotBlank(courier.getCourierNum())){   //使用工具类判断前端传入的数值是否为空
                    Predicate p1 = criteriaBuilder.equal(root.get("courierNum"), courier.getCourierNum());
                    list.add(p1);
                }
                //模糊查询所属单位
                if (StringUtils.isNotBlank(courier.getCompany())){
                    Predicate p2 = criteriaBuilder.like(root.get("company").as(String.class), "%"+courier.getCompany()+"%");
                    list.add(p2);
                }
                //模糊查询类型
                if (StringUtils.isNotBlank(courier.getType())){
                    Predicate p3 = criteriaBuilder.equal(root.get("type"), courier.getType());
                    list.add(p3);
                }
                //关联查询standard表
                Join<Object, Object> standardRoot = root.join("standard", JoinType.INNER);
                //多表关联查询standard，收派名称模糊查询
                if (courier.getStandard()!=null&&StringUtils.isNotBlank(courier.getStandard().getName())){
                    Predicate p4 = criteriaBuilder.like(standardRoot.get("name").as(String.class), "%"+courier.getStandard().getName()+"%");
                    list.add(p4);
                }
                return criteriaBuilder.and(list.toArray(new Predicate[0]));
            }
        };
        Page<Courier> pageData = courierService.findPageData(specification,pageable);

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",pageData.getTotalElements());
        map.put("rows",pageData.getContent());
        //将值压栈
        ActionContext.getContext().getValueStack().push(map);

        return SUCCESS;
    }

    //属性驱动ids
    private String ids;
    public void setIds(String ids) {
        this.ids = ids;
    }

    @Action(value = "courier_delBatch",results = {@Result(name = "success",
                location = "./pages/base/courier.html",type = "redirect")})
    public String delBatch(){
        String[] split = ids.split(",");
        courierService.delBatch(split);
        return SUCCESS;
    }


    //查询未关联的快递员courier_findnoassociation
    @Action(value = "courier_findnoassociation",results = {@Result(name = "success",type = "json")})
    public String findnoassociation(){
        List<Courier> list = courierService.findnoassociation();
        ActionContext.getContext().getValueStack().push(list);
        return SUCCESS;
    }




}
